<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>扩展接口</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="internals2.counter.examples.basic.html">简单接口</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="internals2.counter.examples.objective.html">对象化接口</a></div>
 <div class="up"><a href="internals2.counter.examples.html">范例</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="internals2.counter.examples.extended" class="section">
    <h2 class="title">扩展接口</h2>
    
    <p class="simpara">
     扩展接口提供小的一整套函数，允许用户根据设置定义任意数量名称唯一的计数器。简单接口可与扩展接口同时使用。
    </p>
    
    <div class="example" id="internals2.counter.examples.extended.ex">
     <p><strong>Example #1 &quot;counter&quot;的扩展接口</strong></p>
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">print_counter_info</span><span style="color: #007700">(</span><span style="color: #0000BB">$counter</span><span style="color: #007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(</span><span style="color: #0000BB">is_resource</span><span style="color: #007700">(</span><span style="color: #0000BB">$counter</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"计数器的名称为&nbsp;'%s'，且%s进行持久化。其当前值为&nbsp;%d.\n"</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">counter_get_meta</span><span style="color: #007700">(</span><span style="color: #0000BB">$counter</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">COUNTER_META_NAME</span><span style="color: #007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">counter_get_meta</span><span style="color: #007700">(</span><span style="color: #0000BB">$counter</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">COUNTER_META_IS_PERSISTENT</span><span style="color: #007700">)&nbsp;?&nbsp;</span><span style="color: #DD0000">''&nbsp;</span><span style="color: #007700">:&nbsp;</span><span style="color: #DD0000">'不'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">counter_get_value</span><span style="color: #007700">(</span><span style="color: #0000BB">$counter</span><span style="color: #007700">));<br />&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print&nbsp;</span><span style="color: #DD0000">"计数器无效!\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br />if&nbsp;((</span><span style="color: #0000BB">$counter_one&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">counter_get_named</span><span style="color: #007700">(</span><span style="color: #DD0000">"one"</span><span style="color: #007700">))&nbsp;===&nbsp;</span><span style="color: #0000BB">NULL</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$counter_one&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">counter_create</span><span style="color: #007700">(</span><span style="color: #DD0000">"one"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">COUNTER_FLAG_PERSIST</span><span style="color: #007700">);<br />}<br /></span><span style="color: #0000BB">counter_bump_value</span><span style="color: #007700">(</span><span style="color: #0000BB">$counter_one</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$counter_two&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">counter_create</span><span style="color: #007700">(</span><span style="color: #DD0000">"two"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">5</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$counter_three&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">counter_get_named</span><span style="color: #007700">(</span><span style="color: #DD0000">"three"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$counter_four&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">counter_create</span><span style="color: #007700">(</span><span style="color: #DD0000">"four"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">COUNTER_FLAG_PERSIST&nbsp;</span><span style="color: #007700">|&nbsp;</span><span style="color: #0000BB">COUNTER_FLAG_SAVE&nbsp;</span><span style="color: #007700">|&nbsp;</span><span style="color: #0000BB">COUNTER_FLAG_NO_OVERWRITE</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">counter_bump_value</span><span style="color: #007700">(</span><span style="color: #0000BB">$counter_four</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">print_counter_info</span><span style="color: #007700">(</span><span style="color: #0000BB">$counter_one</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_counter_info</span><span style="color: #007700">(</span><span style="color: #0000BB">$counter_two</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_counter_info</span><span style="color: #007700">(</span><span style="color: #0000BB">$counter_three</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_counter_info</span><span style="color: #007700">(</span><span style="color: #0000BB">$counter_four</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
     </div>

     <div class="example-contents"><p>
      首次运行时，以上例子的输出为：
     </p></div>
     <div class="example-contents screen">
<div class="cdata"><pre>
计数器的名称为 &#039;one&#039;，且进行持久化。其当前值为 2.
计数器的名称为 &#039;two&#039;，且不进行持久化。其当前值为 5.
计数器无效!
计数器的名称为 &#039;four&#039;，且进行持久化。其当前值为 3.</pre></div></div>
     <div class="example-contents"><p>
      在同一 PHP 实例中第 2 次运行时的输出为:
     </p></div>
     <div class="example-contents screen">
<div class="cdata"><pre>
计数器的名称为 &#039;one&#039;，且进行持久化。其当前值为 4.
计数器的名称为 &#039;two&#039;，且不进行持久化。其当前值为 5.
计数器无效!
计数器的名称为 &#039;four&#039;，且进行持久化。其当前值为 4.</pre></div></div>
     <div class="example-contents"><p>
      <em class="emphasis">在另一个 PHP 实例中</em>第 3 次运行时的输出为：
     </p></div>
     <div class="example-contents screen">
<div class="cdata"><pre>
计数器的名称为 &#039;one&#039;，且进行持久化。其当前值为 2.
计数器的名称为 &#039;two，且不进行持久化。其当前值为 5.
计数器无效!
计数器的名称为 &#039;four&#039;，且进行持久化。其当前值为 5.</pre></div></div>
    </div>
   </div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="internals2.counter.examples.basic.html">简单接口</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="internals2.counter.examples.objective.html">对象化接口</a></div>
 <div class="up"><a href="internals2.counter.examples.html">范例</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
